<?php

$db_obj = new db_obj();
$client_obj = new client_obj();
$user_obj = new user_obj();
$filter_obj = new filter_obj();
$tariff_obj = new tariff_obj();
$record_obj = new record_obj();

if (isset($_REQUEST['action'])) {
    switch ($_REQUEST['action']) {
        case 'deleteTariffs':
            if (!empty($_REQUEST['tariffId'])) {
                $success = true;
                foreach ($_REQUEST['tariffId'] as $key => $id) {
                    $res = $tariff_obj->deleteTariffById($id);
                    if (error_obj::isError($res)) {
                        error_obj::setError($res, UNEXCEPTED);
                        $success = false;
                    }
                }
                if ($success){
                    error_obj::setGood("Tariffs were deleted!");
                    base_obj::redirect(ROOT . '?input=list_of_tariffs');
                }
            }
            break;
    }
}

if (!isset($_SESSION['list_of_tariffs_pager'])) {
    $_SESSION['list_of_tariffs_pager'] = $GLOBALS['list_of_tariffs_pager'];
}

if (!isset($_SESSION['list_of_tariffs_order'])) {
    $_SESSION['list_of_tariffs_order'] = $GLOBALS['list_of_tariffs_order'];
}

if (isset($_SESSION['list_of_tariffs_fulltext']) && !empty($_SESSION['list_of_tariffs_fulltext']))
    $GLOBALS['template_data']['currentFulltext'] = $_SESSION['list_of_tariffs_fulltext'];

$filter = array();

$currentFilter = array();

if (isset($_SESSION['list_of_tariffs_filter'])) {
    $filter = $_SESSION['list_of_tariffs_filter'];
    $result = $filter_obj->updateFilter($_SESSION['user']['user_id'], 'list_of_tariffs_filter', $filter);
    if (error_obj::isError($result)) {
        error_obj::setError($result, UNEXCEPTED);
    }
    foreach ($_SESSION['list_of_tariffs_filter'] as $f => $v) {
        switch ($f) {
            case 'title':
                $currentFilter[$f] = $v;
                break;
            case 'tariff':
                $currentFilter[$f] = $v;
                break;
            case 'servis_tariff':
                $currentFilter[$f] = $v;
                break;
            case 'unit':
                $currentFilter[$f] = $v;
                break;
            case 'type_of_record':
                $currentFilter[$f] = $record_obj->getTypeOfRecordById($v);
                $currentFilter[$f] = $currentFilter[$f]['type_of_record'];
                break;
        }
    }
}

$GLOBALS['template_data']['currentFilter'] = $currentFilter;

if (!isset($_GET['page'])) {
    $_GET['page'] = 0;
}

$limit = ($_GET['page'] * $_SESSION['list_of_tariffs_pager']) . ' ,' . $_SESSION['list_of_tariffs_pager'];

if (isset($_SESSION['list_of_tariffs_fulltext']) && !empty($_SESSION['list_of_tariffs_fulltext'])){
    $PAGER_COUNT = $tariff_obj->getTariffsByFulltext($_SESSION['list_of_tariffs_fulltext'],$filter, $_SESSION['list_of_tariffs_order'], '', 'COUNT(*)');
    $tariffs = $tariff_obj->getTariffsByFulltext($_SESSION['list_of_tariffs_fulltext'],$filter, $_SESSION['list_of_tariffs_order'], $limit);
} else {
    $PAGER_COUNT = $tariff_obj->getTariffs('COUNT(*)', $filter, $_SESSION['list_of_tariffs_order'], '');
    $tariffs = $tariff_obj->getTariffs('*', $filter, $_SESSION['list_of_tariffs_order'], $limit);
} 
$PAGER_COUNT = $PAGER_COUNT[0][0];
$GLOBALS['template_data']['total_count'] = $PAGER_COUNT;
$GLOBALS['template_data']['currentOrder'] = array(
    $_SESSION['list_of_tariffs_order']['col'],
    $_SESSION['list_of_tariffs_order']['order']
);

foreach ($tariffs as $key => $tariff) {
    $tariffs[$key]['type_of_record_info'] = $record_obj->getTypeOfRecordById($tariff['type_of_record']);
}


$GLOBALS['template_data']['tariffs'] = $tariffs;

$pager = $db_obj->pager($PAGER_COUNT, $_GET['page'], $_SESSION['list_of_tariffs_pager']);

$GLOBALS['template_data']['pager'] = $pager;
?>
